<template>
	<view class="addr-list">
		<view class="addr-item" v-for="(item,index) in addressList" :key="index">
			<view class="nickname-phone" @click="selectAddr(item)">
				<text class="default-addr" v-if="item.isDefault==='1'">默认</text>
				<text class="nickname">{{item.contact}}</text><text class="phone">{{item.phone}}</text>
			</view>
			<view class="addr" @click="selectAddr(item)">
				{{item.province}}{{item.city}}{{item.area}}
				{{item.address}}
			</view>

			<image src="/static/icons/address_modify_botton@3x.png" class="icon-edit" @click="editAddressInfo(item)">
			</image>
		</view>

		<view class="add-new-address" @click="addAddress">
			新增收货地址
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				addressList: [], // 收货地址
				lastPage: '' // 来自哪个页面
			};
		},
		onLoad() {
			console.log(this.userInfo)
			// this.getAddressList(this.userInfo._id)
		},
		onShow() {
			console.log('selectAddrPage')
			this.getAddressList(this.userInfo._id)
		},
		methods: {
			...mapMutations('m_user', ['updateAddress']),
			// 选中地址
			selectAddr(item) {
				console.log(item)
				uni.setStorageSync('isChanged', true)

				this.updateAddress(item)
				var pages = getCurrentPages(); // 当前页面
				var prevPage = pages[pages.length - 2].route; // 上一个页面
				if (prevPage == 'subpkg/cart-order/cart-order') {
					uni.navigateBack({
						delta: 0
					})
				}
			},
			// 获取收货地址
			async getAddressList(id) {
				const {
					data: res
				} = await uni.$http.post('/cart/getAddressList', {
					userId: this.userInfo._id
				})
				// console.log(res)
				this.addressList = res.data
			},
			// 跳转到编辑地址页面
			editAddressInfo(info) {
				uni.navigateTo({
					url: '/subpkg/update-address/update-address?info=' + JSON.stringify(info)
				})
			},
			// 跳转到新增地址页面
			addAddress() {
				uni.navigateTo({
					url: '/subpkg/add-address/add-address'
				})
			}
		},
		computed: {
			...mapState('m_user', ['address', 'userInfo']),
			...mapGetters('m_user', ['addressStr'])
		}
	}
</script>

<style lang="scss">
	.addr-list {
		padding-top: 6rpx;
		padding-bottom: 200rpx;
	}

	.nickname-phone {
		padding-top: 10rpx;
		box-sizing: border-box;
	}

	.addr-item {
		height: 120rpx;
		// background-color: #eee;
		padding: 0 24rpx;
		position: relative;
		margin-top: 50rpx;

		.icon-edit {
			position: absolute;
			width: 33rpx;
			height: 37rpx;
			right: 17rpx;
			top: 20rpx;
		}

		.nickname {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}

		.default-addr {
			display: inline-block;
			width: 61rpx;
			height: 29rpx;
			line-height: 29rpx;
			text-align: center;
			font-size: 24rpx;
			font-weight: bold;
			color: #FFFFFF;
			background: #D80E0E;
			margin-right: 20rpx;
		}

		.phone {
			font-size: 32rpx;
			font-weight: 500;
			color: #333333;
			margin-left: 32rpx;
		}

		.addr {
			font-size: 28rpx;
			font-weight: 500;
			color: #666666;
			// margin-top: 26rpx;
			width: 670rpx;
			overflow: hidden;
			height: 61rpx;
			line-height: 80rpx;
			white-space: nowrap;
			text-overflow: ellipsis;
		}
	}

	.add-new-address {
		position: fixed;
		bottom: 0;
		width: 750rpx;
		height: 98rpx;
		line-height: 98rpx;
		text-align: center;
		background: #FFFFFF;
		box-shadow: 0px 2rpx 8rpx 1rpx rgba(0, 0, 0, 0.3);
	}
</style>
